package com.sct.tsdemo;

import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;

import java.io.IOException;
import java.util.List;

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
    final String TAG = "SCT-DEBUG";
    private Camera mCam;
    private RelativeLayout mPreview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        SurfaceView sv = new SurfaceView(this);
        sv.getHolder().addCallback(this);

        mPreview = (RelativeLayout)findViewById(R.id.preview);
        mPreview.addView(sv);

        int numberOfCameras = Camera.getNumberOfCameras();
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        for (int i = 0; i < numberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                mCam = Camera.open( i );
            }
        }

    }

//    @Override
//    public void onSurfaceCreated(SurfaceHolder holder)
//    {
//
//    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.v(TAG, "on surfaceCreated");

        try {
            mCam.setPreviewDisplay(holder);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Camera.Parameters parameters = mCam.getParameters();



        List<Camera.Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
        List<Camera.Size> supportedPictureSizes = parameters.getSupportedPictureSizes();

        int sh = 10000;
        int sw = 10000;

        for (int i = 0; i<supportedPreviewSizes.size();i++)
        {
//                Log.v("sct", String.format("%d %d", i, supportedPreviewSizes.size()));
//                Log.v("sct", String.format("supportedPreviewSizes:%dx%d", supportedPreviewSizes.get(i).width, supportedPreviewSizes.get(i).height));

            if(supportedPreviewSizes.get(i).width < sw && supportedPreviewSizes.get(i).width >= 320)
            {
                ///check if pic size support
                for (int j = 0; j<supportedPictureSizes.size();j++)
                {
//                        Log.v("sct", String.format("%d %d", j, supportedPictureSizes.size()));
//                        Log.v("sct", String.format("supportedPictureSizes:%dx%d", supportedPictureSizes.get(j).width, supportedPictureSizes.get(j).height));

                    if(supportedPictureSizes.get(j).width == supportedPreviewSizes.get(i).width &&
                            supportedPictureSizes.get(j).height == supportedPreviewSizes.get(i).height)
                    {
                        ///choose it
                        sw = supportedPreviewSizes.get(i).width;
                        sh = supportedPreviewSizes.get(i).height;
                    }
                }

            }

        }

//        parameters.setFlashMode("off"); // 无闪光灯
//        parameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
//        parameters.setSceneMode(Camera.Parameters.SCENE_MODE_AUTO);
//        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        parameters.setPreviewFormat(ImageFormat.YV12);
        parameters.setPreviewFrameRate(25);
        parameters.setPictureSize(sw, sh);
        parameters.setPreviewSize(sw, sh);
        //这两个属性 如果这两个属性设置的和真实手机的不一样时，就会报错
        mCam.setParameters(parameters);

        mCam.startPreview();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
}
